package org.project.d.register.nacos;

import com.alibaba.boot.nacos.discovery.NacosDiscoveryConstants;
import com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import static com.alibaba.nacos.spring.util.NacosBeanUtils.DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME;

/**
 * @author lin
 * @version V1.0
 * @description
 * @date 2020/9/21 5:24 下午
 */
@ConditionalOnProperty(name = NacosDiscoveryConstants.ENABLED, matchIfMissing = true)
@ConditionalOnMissingBean(name = DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME)
@EnableNacosDiscovery
@AutoConfigureBefore(com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration.class)
@EnableConfigurationProperties(value = NacosDiscoveryProperties.class)
@ConditionalOnClass(name = "org.springframework.boot.context.properties.bind.Binder")
public class NacosDiscoveryAutoConfiguration {

    @Bean
    public NacosDiscoveryAutoRegister discoveryAutoRegister() {
        return new NacosDiscoveryAutoRegister();
    }
    @Bean
    public NacosDiscoveryAutoDeregister discoveryAutoRegister(NacosDiscoveryProperties discoveryProperties,
                                                              NacosDiscoveryAutoRegister nacosDiscoveryAutoRegister) {
        return new NacosDiscoveryAutoDeregister(discoveryProperties,nacosDiscoveryAutoRegister);
    }

    @Bean
    public RegisterServer registerServer(){
        return new RegisterServer();
    }

}